

#include "threadsafe.h"
#include "finder.h"
#include "time.h"
#include "properties.h"
#include "dbms.h"
#include "htmlobject.h"
#include "url.h"


#include "logo.h"

class logo:public URLhandler{
   public:
   logo() : URLhandler("caravan.logo"){}
   void output(genericObject *fobj,Runnable* r,File *of){
      htmlObject *obj=new htmlObject(new Buffer(sRESPONSE,strlen(sRESPONSE)+1));
      obj->putValue("Content-type","image/gif");
      obj->putValue("cache-control","cache");
      addObject(obj,fobj);
      of->write((char*)logobuff,sizeof(logobuff));
      }
   };

extern char* versionString;

class caravanMenu:public URLhandler{
   public:
   caravanMenu() : URLhandler("caravan.html"){}
   void output(genericObject *fobj,Runnable*,File *f);
   void mainMenu(File*,genericObject*);
   void loginMenu(File*);
   };

void caravanMenu::output(genericObject *fobj,Runnable* r,File *of)
{
   of->write("<html><head><title>Caravan Home Page</title></head><body bgcolor=336699 text=eecc33 link=ffffff vlink=ffffff alink=ffffff><center><h1>Caravan Business Server</h1>");
   of->write("<center><font color=ffffcc><b>One stop solution for the web</center>");
   if(checkDomain(fobj,"admin"))
    mainMenu(of,fobj);
   else
    loginMenu(of);
   of->write("</body></html>");
}

extern char* sNEWPSWD;

void caravanMenu::mainMenu(File* of,genericObject* fobj)
{
   of->write("<table bgcolor=#336699 cellpadding=0 cellspacing=0 border=0><tr><td rowspan=8 align=left valign=bottom><img src=\"caravan.logo\"><br><font size=2><b>Caravan v");
   of->write(versionString);
   of->write("</td><td rowspan=8 width=20 bgcolor=336699><br></td>\n");
   htmlObject *ui=(htmlObject*)getObject(sUSERINFO,fobj);
   int no=0;
   if(ui==0){
		of->write("<html><body bgcolor=336699 link=\"ffffff\" vlink=ffffff><center><h1><font color=ffee66><a href=\"caravan.html\">CARAVAN TEMPLATE MANAGER</h1></a>Access Denied,Please Login<br>");
		return ;
   }else{
	   	 if((no=ui->getValue(sDOMAIN,sNEWPSWD))!=0){
			  of->write("<td><font color=yellow><b>Please change password<br>You are using default password<br>Administrator Access will be denied!</td></tr>\n");
		 }
   }
//   of->write("<td><A href=\"/caravan.html?sel=config\"><b>Configure Caravan Server</a></td></tr>\n");
//   of->write("<tr><td>Restart  Server</td></tr>");
//   of->write("<tr><td>Server Information</td></tr>");
   of->write("<tr><td><a href=\"dbms.html\"><b>Caravan Data Base Manager</a></td></tr>");
   of->write("<tr><td><a href=\"dbmanager.html\"><b>DBManager(Beta)</a></td></tr>");
   of->write("<tr><td><a href=\"createuser.html\"><b>Create A User</a></td></tr>");
   of->write("<tr><td><a href=\"edit_tmpl.html\"><b>Edit Caravan Scripts</a></td></tr>");
   of->write("<tr><td><a href=\"password.html\"><b>Change Your Password</a></td></tr>");
   of->write("<tr><td><a href=\"logout.html?caller=caravan.html\"><b>Logout</a></td></tr>");
   of->write("</table>");
}


void caravanMenu::loginMenu(File* of)
{
   of->write("<table bgcolor=#336699 cellpadding=0 cellspacing=0 border=0><tr><td rowspan=6 align=left valign=bottom><img src=\"caravan.logo\"><br><font size=2><b>Caravan v");
   of->write(versionString);
   of->write("</td><td rowspan=6 width=20 bgcolor=336699><br></td>\n");
   of->write("<tr><td><b>Administrator login required<br> to access this page</td></tr>");
   of->write("<tr><td><a href=\"login.html?caller=caravan.html\"><b>Login</a></td></tr>");
   of->write("</table>");
}


class tmpm:public URLhandler{
   public:
   tmpm() : URLhandler("edit_tmpl.html"){}
   void output(genericObject *,Runnable*,File*);
};

void dhtmlRegisterFile(File*);

int isScheduler(char*);
int isEventHandler(char*);
void tmpm::output(genericObject *fobj,Runnable*,File *of)
{
	char tmp[256];
	char *filetoedit=0;
	htmlObject *ui=(htmlObject*)getObject(sUSERINFO,fobj);
	if(ui==0 || ui->getValue(sDOMAIN,sNEWPSWD)){
		of->write("<html><body bgcolor=336699 link=\"ffffff\" vlink=ffffff><center><h1><font color=ffee66><a href=\"caravan.html\">CARAVAN TEMPLATE MANAGER</h1></a>Access Denied<br>");
		return ;
	}
	if(checkDomain(fobj,"admin")){
		of->write("<html><head><title>Caravan Template Manager</title></head><body bgcolor=336699 text=eecc33 link=ffffff vlink=ffffff alink=ffffff><center><a href=caravan.html><h1><font color=gold face=arial>Caravan Business Server</h1></a><b><b>");
		htmlObject *form=(htmlObject*)getObject(sFORM,fobj);
		if(form){
			filetoedit=form->showValue("c");
			if(filetoedit!=0){
				if(*filetoedit){
					snprintf(tmp,256,"%s%c%s",(char*)(*serverConfig)["templates"],FSLASH,filetoedit);
					RealFile *f=new RealFile(tmp);
					if(f->exists()==0){
						f->create();
						if(f->exists()){
							f->close();
							dhtmlRegisterFile(f);
						}
					}
					delete f;
				}
				goto editFile;
			}else{
				filetoedit=form->showValue("s");
editFile:
				if(filetoedit && *filetoedit){
					snprintf(tmp,256,"%s%c%s",(char*)(*serverConfig)["templates"],FSLASH,filetoedit);
					File *f=new File(tmp);
					if(f->exists()){
						of->write("<table><tr><td><b>");
						of->write(filetoedit);
						of->write("</td><form>\r\n");
						of->write("<td><input type=submit name=action value=\"CANCEL\"></td></form><form name=editor action=edit_tmpl.html method=post enctype=multipart/form-data>\r\n");
						of->write("<td><input type=submit name=action value=\"Save File\"></td>");
						of->write("<input type=hidden name=filename value=\"");
						of->write(filetoedit);
						of->write("\"></tr></table><hr><font size=3><textarea name=content cols=150 rows=36 wrap=no>");
						int n=0;
						do{
							n=f->read(tmp,256);
							of->write(tmp,n);
						}while(n==256);
						of->write("</textarea>\r\n");
						of->write("</form>\r\n");     
						snprintf(tmp,256,"<hr><font size=1><b>Caravan Business Server ver %s &copy All Rights Reserved</body></html>",versionString);
						of->write(tmp);
					}else{
						of->write("<b><font color=red>File Not Found -- <a href=\"edit_tmpl.html?c=");
						of->write(filetoedit);
						of->write("\">Create File</a></body></html>");
					}
					delete f;
				}else{
					char *action=form->showValue("action");
					if(action && (stricmp(action,"save file")==0)){
						filetoedit=form->showValue("filename");
						char *content=form->showValue("content");
						if(filetoedit && *filetoedit && content && *content){
							snprintf(tmp,256,"%s%c%s",(char*)(*serverConfig)["templates"],FSLASH,filetoedit);
							RealFile *f=new RealFile(tmp);
							if(f->exists()){
								f->replace();
								f->write(content);
								f->close();
							}
							char *url=f->getRealName();
							if(getURLhandler(url)==0){
								dhtmlRegisterFile(f);
							}
							delete f;
						}
					}
					goto fileList;
				}
				return ;
			}
		}else{
fileList:
		finder* f = new finder((*serverConfig)["templates"]);
		int i=1;
		of->write("<table border=0 cellspacing=4 cellpadding=0><tr>");
		if (f->findFile("*")){
			f->sortFiles();
			do{
				of->write("<td><a href=\"edit_tmpl.html?s=");
				of->write(f->getName());
				of->write("\">");
				if(filetoedit && stricmp(filetoedit,f->getName())==0)
					of->write("<font color=Yellow><b>");
				else
					if(::isEventHandler(f->getName()))
						of->write("<font color=ff6633><b>");
					else
						if(::isScheduler(f->getName()))
							of->write("<font color=99ee66><b>");
						of->write(f->getName());     
						int n=getActiveCount(f->getName());
						if(n!=0){
							char t[20];
							snprintf(t,20,"/<b>%d</b>",n);
							of->write(t);
						}
						of->write("</a></td>");     
						i++;
						if(i%4==0)
							of->write("</tr><tr>\r\n");
			}while(f->find());
		}
		delete f;
		of->write("</tr></table></center><hr></b><font size=1><font color=Yellow>Edited<font color=ee6633> EventHandler<font color=99ee66> Scheduler</sub><b><center><font color=gold>Caravan Business Server ");
        of->write(versionString);
		of->write(" &copy All Rights Reserved</body></html>");
		return ;
		}
	}
}

extern void initcachelog();
void initCaravan()
{
	new logo();
	new caravanMenu();
	new tmpm();
	initcachelog();
	
}

